package com.erp.shiro.impl;

import com.erp.common.BaseContext;
import com.erp.common.SuperConstant;
import com.erp.entity.User;
import com.erp.entity.vo.ViewAction;
import com.erp.entity.vo.ViewManager;
import com.erp.service.UserService;
import com.erp.shiro.ShiroRealm;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
public class ShiroRealmImpl extends ShiroRealm {

    @Autowired
    private UserService userService;

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.info("认证");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //根据用户名查找对象
        User user = userService.findByUsername(token.getUsername());
        //把查找到的对象返回
        if (ObjectUtils.isEmpty(user)) {
            throw new UnknownAccountException("用户不存在");
        }
        String salt = user.getSalt();
        String password = user.getPassword();
        System.out.println(password);
        return new SimpleAuthenticationInfo(user, password, ByteSource.Util.bytes(salt),getName());
    }

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("授权");
        User user = (User) principalCollection.getPrimaryPrincipal();
        BaseContext.setCurrentId(user.getId());
        ViewManager manager = userService.findManagerByUsername(user.getUsername());
        if (manager != null) {
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            simpleAuthorizationInfo.addRole(manager.getManager());
            List<ViewAction> viewActionList = userService.findActionByManagerId(manager.getManager());
            if (!CollectionUtils.isEmpty(viewActionList)) {
                viewActionList.forEach(viewAction ->
                        simpleAuthorizationInfo.addStringPermission(viewAction.getAction()));
            }
            return simpleAuthorizationInfo;
        }
        return new SimpleAuthorizationInfo();
    }

    @Override
    public void initCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(SuperConstant.HASH_ALGORITHM);
        hashedCredentialsMatcher.setHashIterations(SuperConstant.HASH_INTERATIONS);
        setCredentialsMatcher(hashedCredentialsMatcher);
    }
}
